<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>动态组件</title>
  <script src="../vue.js"></script>
</head>
<body>
  <div id="app">
    <!-- <child v-if="swap === 'child'">this is child component</child>
    <child2 v-else></child2> -->
    <component :is="currentComponent"></component>
    <div>
      <button @click="handleSwap">swap</button>
    </div>
  </div>
  <script type="module">
    import './child.js'
    import './child2.js'
    new Vue({
      el: '#app',
      data: {
        swap: 'child',
        currentComponent: 'child'
      },
      methods: {
        handleSwap() {
          // if (this.swap === 'child') {
          //   this.swap = 'child2'
          // } else {
          //   this.swap = 'child'
          // }
          if (this.currentComponent === 'child') {
            this.currentComponent = 'child2'
          } else {
            this.currentComponent = 'child'
          }
        }
      },
    })
  </script>
</body>
</html>